| CIT 590 Midterm Exam, Fall 2016 |
Name ______________________________ |
Please keep all answers short and to the point. Do not add information that is not asked for; you will not gain points, but you may lose points if you get it wrong
| def any_lowercase1(s): for c in s: if c.islower(): return True else: return False | Result is based only on first letter of string
s |
| def any_lowercase2(s): for 'c' in s: if c.islower(): return 'True' else: return 'False' | SyntaxError: can't assign to literal |
| def any_lowercase3(s): for c in s: flag = c.islower() return flag | Result is based only on last letter of string
s |
| def any_lowercase4(s): flag = False for c in s: flag = flag or c.islower() return flag | Works correctly. |
| def any_lowercase5(s): for c in s: if not c.islower(): return False return True | Return True only if all letters in
s are lowercase. |
Write the header for a method named computer_move
that takes no parameters. |
def computer_move(): |
| Write a statement to print "Computer's turn", preceded by a
blank line |
print("\nComputer's turn") |
Set an integer variable named sum to zero. |
sum = 0 |
Start a loop that will run until sum is 17 or
larger. |
while sum < 17: |
Set a variable roll to a random integer between
1 and 6, inclusive. (Assume random has been
imported.) |
roll = random.randint(1, 6) |
| Print out a message saying what the computer has just rolled. |
print("The computer rolls", |
| Test if the number rolled is a 1 . . . |
if |
| And if it is, return a zero. |
return 0 |
| . . . but if it isn't a 1 . . . |
else: |
Add the number rolled to sum. |
sum += |
| After the loop is finished, print out a message saying how
much the computer has gained this turn. |
print("The computer gets", sum) |
| Return from the function with the amount gained this turn. |
return sum |
abc = "abcdefg"
abc[2:4] ? 'cd'abc[5:] ? 'fg'abc[:5] ? 'abcde'abc[-3 : -1] ? 'ef'abc * 3 ?
'abcdefgabcdefgabcdefg'cards = {"ace" : 1, "deuce" : 2, "trey" : 3} for i in cards:
print(i)
ace
deuce
trey
lambda x: 2 * x or lambda x: x + x
lambda x: x // 2 if x % 2 == 0 else 3 * xformat method
to print out a floating point number x with two digits
after the decimal point.print('{:.2f}'.format(x)) {7.2f}, {:.2e}, {7.2g},
etc.Open a file named data.txt for
reading. |
stream = open("data.txt", "r") |
| Read in the entire file as a list of lines. |
lines = stream.readlines() |
| Close the file. |
stream.close() |
a = [1, 2, [3, 4]]
b = a[:]
a[2] = [5, 6]b?squares to a
list of the squares of the numbers 1 through 1000.squares = [x * x for x in range(1, 1001)]small is defined as: def small(a, b=5):
return a < blist(map(small, [1, 2, 3, 4, 5]))
[True, True, True, True, False]
list(filter(small, [1, 2, 3, 4, 5]))
[1, 2, 3, 4]
reduce(small, [1, 2, 3, 4, 5])
True (give one point for [True])"Click me" and (2) when clicked, calls the
function clicked().